package com.sdry.service.impl.zc;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sdry.mapper.zc.ZcInventoryManagementMapper;
import com.sdry.mapper.zc.ZcRejectsWarehouseMapper;
import com.sdry.model.llm.LlmWarehouseStock;
import com.sdry.model.lz.Warehouse;
import com.sdry.model.lz.WarehouseRegion;
import com.sdry.model.zc.ZcGeneralQueryEntity;
import com.sdry.model.zc.ZcRejectsWarehouseEntity;
import com.sdry.service.zc.ZcRejectsWarehouseService;

/**
 * 不良库管理
 * @ClassName:    ZcRejectsWarehouseServiceImpl   
 * @Description:  
 * @Author:       zc   
 * @CreateDate:   2019年4月25日 下午5:19:32   
 * @Version:      v1.0
 */
@Service
public class ZcRejectsWarehouseServiceImpl implements ZcRejectsWarehouseService {

	@Resource
	private ZcRejectsWarehouseMapper zcRejectsWarehouseMapper;
	@Resource
	private ZcInventoryManagementMapper zcInventoryManagementMapper;
	/**
	 * 查询不良库列表
	 */
	@Override
	public List<ZcRejectsWarehouseEntity> selectRejectsWarehouseList(ZcGeneralQueryEntity zcGeneralQueryEntity) {
		//表名
		String tab = "zc_rejects_warehouse info left join lz_materiel m on m.id = info.mid left join zc_system_user u on u.id = info.enterPerson";
		//查询字段
		String strFld = "info.*,m.materiel_name, m.materiel_num,m.brevity_num,m.packing_quantity,m.unit,u.userName";
		//排序
		String sort = "info.id";
		//0返回结果集，1返回count数
		byte isGetCount = 0;
		zcGeneralQueryEntity.setTab(tab);
		zcGeneralQueryEntity.setSort(sort);
		zcGeneralQueryEntity.setIsGetCount(isGetCount);
		zcGeneralQueryEntity.setStrFld(strFld);
		return zcRejectsWarehouseMapper.selectRejectsWarehouseList(zcGeneralQueryEntity);
	}
	/**
	 * 查询不良库数量
	 */
	@Override
	public int countRejectsWarehouseList(ZcGeneralQueryEntity zcGeneralQueryEntity) {
		//表名
		String tab = "zc_rejects_warehouse info left join lz_materiel m on m.id = info.mid";
		//0返回结果集，1返回count数
		byte isGetCount = 1;
		zcGeneralQueryEntity.setTab(tab);
		zcGeneralQueryEntity.setIsGetCount(isGetCount);
		return zcRejectsWarehouseMapper.countRejectsWarehouseList(zcGeneralQueryEntity);
	}
	/**
	 * 不良品入库
	 */
	@Override
	public int enterRejects(ZcRejectsWarehouseEntity zcRejectsWarehouseEntity) {
		int res = 0;
		//判断有无目标库绑定
		if(zcRejectsWarehouseMapper.countInventoryInfoByRegionId(zcRejectsWarehouseEntity)>0){
			res = zcRejectsWarehouseMapper.addNum(zcRejectsWarehouseEntity);
		}else{
			res = zcRejectsWarehouseMapper.bindingNewRejectsWare(zcRejectsWarehouseEntity);
		}
		return res;
	}
	/************************************APP************************************/
	/**
	 * 查询不良库列表数量
	 */
	@Override
	public List<ZcRejectsWarehouseEntity> selectRejectsWarehouseListApp(String mcode) {
		return zcRejectsWarehouseMapper.selectRejectsWarehouseListApp(mcode);
	}
	/**
	 * 查询不良品仓库列表
	 */
	@Override
	public List<Warehouse> getBadnessWarehouse() {
		ZcGeneralQueryEntity zcGeneralQueryEntity = new ZcGeneralQueryEntity();
		//表名
		String tab = "lz_warehouse";
		//查询条件
		String strWhere = "";
		//排序
		String sort = "id";
		//0返回结果集，1返回count数
		byte isGetCount = 0;
		zcGeneralQueryEntity.setTab(tab);
		zcGeneralQueryEntity.setSort(sort);
		zcGeneralQueryEntity.setIsGetCount(isGetCount);
		zcGeneralQueryEntity.setStrWhere(strWhere);
		List<Warehouse> wList = zcInventoryManagementMapper.selectAllWarehouseList(zcGeneralQueryEntity);
		for (Warehouse warehouse : wList) {
			//表名
			tab = "lz_warehouse_region";
			//查询条件
			strWhere = "warehouse_id = "+ warehouse.getId() + "and region_type = -1 and";
			//排序
			sort = "id";
			//0返回结果集，1返回count数
			isGetCount = 0;
			zcGeneralQueryEntity.setTab(tab);
			zcGeneralQueryEntity.setSort(sort);
			zcGeneralQueryEntity.setStrWhere(strWhere);
			zcGeneralQueryEntity.setIsGetCount(isGetCount);
			List<WarehouseRegion> rList = zcInventoryManagementMapper.selectRejectsWarehouseRegionListByWarehouseId(zcGeneralQueryEntity);
			warehouse.setWarehouseRegion(rList);
		}
		return wList;
	}

}
